// JavaScript Document
// MUST include jquery-1.6.2

var currentDrawerID = "";
var drawerOpen = "false";

function switchToDiv(dest, source){
	$("#"+dest).fadeOut(300,function(){
		changeDivContentFadeIn(dest,source)
		});
}


function changeDivContentFadeIn(dest, source){
	
	document.getElementById(dest).innerHTML = document.getElementById(source).innerHTML;
	$("#"+dest).fadeIn(300);
	dynamicDragDropInit();
}

function changeDivContent(dest, source){
	document.getElementById(dest).innerHTML = document.getElementById(source).innerHTML;
}

var TimeToFade = 1000.0;

function fade(eid)
{
  var element = document.getElementById(eid);
  if(element == null)
    return;
    
   
  if(element.FadeState == null)
  {
    if(element.style.opacity == null || element.style.opacity == '' 
       || element.style.opacity == '1')
      element.FadeState = 2;
    else
      element.FadeState = -2;
  }
    
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  }  
}

function animateFade(lastTick, eid)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
  
  var element = document.getElementById(eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = ' + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
  
  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}

function drawerSwitch(dest, source){
	if(source != currentDrawerID && drawerOpen){
		switchToDiv(dest, source);
		currentDrawerID = source;
	}else if(drawerOpen && source == currentDrawerID){
		toggleSlide();
		drawerOpen = false;
	}else{
		changeDivContent(dest,source);
		toggleSlide();
		currentDrawerID = source;
		drawerOpen = true;
	}
}

function toggleSlide()
{
    var drawer = $("#drawer");
    var content = $("#inventory_slideout_drawer");
    if (jQuery(content).is(":hidden"))
    {
        // The order here is important, we must slide juste before starting blinding
        setTimeout(function()
                    {
                        jQuery(content).effect("slide", { direction: "up", mode : "show"}, 500);
                    },1);
        setTimeout(function()
                    {
                        jQuery(drawer).effect("blind", { direction: "vertical", mode : "show" }, 490);

                    },1);
    }
    else
    {
        setTimeout(function()
                    {
                        jQuery(drawer).effect("blind", { direction: "vertical", mode : "hide" }, 500);
                        // The previous blind effect hide the drawer
                        // However we want it to be shown again, if not the next "drawer opening effect" won't play
                        jQuery(drawer).effect("blind", { direction: "vertical", mode : "show" }, 1);
                    },1);
        setTimeout(function()
                    {
                        jQuery(content).effect("slide", { direction: "up", mode : "hide" }, 460);
                    },1);
    }
}

